<template>
  <div class="app-container">
    <!-- 返回上一页按钮 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
      @submit.native.prevent>
      <el-button type="info" @click="goBack">返回</el-button>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- 其他表单项 -->
      <el-form-item prop="status">
        <el-button type="success" plain icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="info" plain icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <div class="info">

    </div>

    <el-table v-loading="loading" :data="bitchList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户姓名" align="center" prop="userName" />
      <el-table-column label="岗位" align="center" prop="sysPostName" />
      <el-table-column label="工区" align="center" prop="spaceName" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.startTime ? scope.row.startTime : '-' }}</span>
        </template>

      </el-table-column>

      <el-table-column label="提交时间" align="center" prop="commitTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.commitTime ? scope.row.commitTime : '-' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="总时长" align="center" prop="answerTime" />

      <el-table-column label="分数" align="center" prop="score">
        <template slot-scope="scope">
          <span>{{ scope.row.score ? scope.row.score : '-' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime ? scope.row.updateTime : '-' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="是否通过" align="center" prop="isPass">
        <template slot-scope="scope">
          <!-- <span>{{ scope.row.isPass === 1 ? '通过' : '未通过' }}</span> -->
          <span v-if="scope.row.isPass == 0">未通过</span>
          <span v-if="scope.row.isPass == 1">已通过</span>
          <span v-if="scope.row.isPass == 2">未答题</span>
          <span v-if="scope.row.isPass == 3">待判卷</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">未开始</span>
          <span v-if="scope.row.status == 1">进行中</span>
          <span v-if="scope.row.status == 2">已完成</span>
          <span v-if="scope.row.status == 3">已结束</span>
          <span v-if="scope.row.status == 4">未完成</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="small" type="text" plan @click="handleView(scope.row)">查看</el-button>
          <el-button size="small" type="text" plan @click="handleUpdate(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 修改分数 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row>
          <el-col :span="12">
            <el-form-item label="卷面分数" prop="score">
              <el-input v-model="form.score" placeholder="分数" disabled />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="试卷总分" prop="parperScore">
              <el-input v-model="form.parperScore" placeholder="分数" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="最后分数" prop="newScore">
              <el-input v-model.number="form.newScore" :max="form.parperScore" placeholder="请输入最后分数"
                @change="handleScoreChange" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog class="pre_dialog" title="预览试卷" :visible.sync="open2" width="660px" append-to-body>
      <preview-parper2 :preData="preData"></preview-parper2>
    </el-dialog>
  </div>
</template>

<script>
import { delBitch } from "@/api/exa/bitch"
import { getUser } from "@/api/system/user"
import { listParper, getParperInfo } from "@/api/exa/parper"
import { listExaResult, setScore, countUser } from "@/api/exa/exaResult"
import Treeselect from "@riophae/vue-treeselect"
import "@riophae/vue-treeselect/dist/vue-treeselect.css"
import previewParper2 from "@/components/parper/index2.vue"

export default {
  components: { Treeselect, previewParper2 },
  dicts: ["space"],
  name: "exaResult",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考试批次表格数据
      bitchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        newScore: [
          { required: true, message: "请输入新分数", trigger: "blur" }
        ],
      },
      // 岗位下拉选项
      postOptions: [],
      // 试卷下拉选项
      parperList: [],

      preData: {}
    }
  },
  created() {
    this.getList();
    this.getParPerList();
    this.getUserList();

    this.countUserInfo();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 接收路由传参的参数
      console.log(this.$route.query);

      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.queryParams.bitchId = Number(this.$route.query.id)
      this.queryParams.sysPostId = Number(this.$route.query.sysPost)
      this.queryParams.spaceId = Number(this.$route.query.space)

      listExaResult(this.queryParams).then(response => {
        console.log("考试批次列表", response);

        this.bitchList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    // 获取岗位列表
    getUserList() {
      getUser().then(response => {
        this.postOptions = response.posts
      })
    },

    // 获取试卷列表
    getParPerList() {
      this.loading = true
      let obj = {
        pageNum: 1,
        pageSize: 999999999,
      }
      listParper(obj).then(response => {
        // console.log("试卷列表", response);

        this.parperList = response.rows
      })
    },

    countUserInfo() {
      countUser({ bitchId: this.$route.query.id }).then(response => {

        console.log("countUser:", response);

      })
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bitchName: null,
        space: null,
        sysPost: null,
        startTime: null,
        endTime: null,
        parperId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log("修改:", row);

      this.open = true
      this.title = "修改"
      this.form = JSON.parse(JSON.stringify(row))

    },

    // 查看按钮操作
    handleView(row) {
      this.open2 = true
      // this.preData = JSON.parse(JSON.stringify(row))

      getParperInfo({
        parperId: row.parperId,
        bitchId: row.bitchId,
        userId: row.userId,
      }).then(response => {
        console.log("getParperInfo:", response);

        this.preData = response.data
      })

      // this.reset()
      // const id = row.id || this.ids
      // getBitch(id).then(response => {
      //   this.preData = response.data
      //   this.open2 = true
      //   console.log("预览数据:", this.preData);
      // })
    },

    handleScoreChange(val) {
      if (val > this.form.parperScore) {
        this.form.newScore = this.form.parperScore;
        this.$message.warning(`分数不能大于试卷总分（${this.form.parperScore}）`);
      }
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {

          if (this.form.newScore > this.form.parperScore) {
            this.$message.error(`分数不能大于试卷总分（${this.form.parperScore}）`);
            return;
          }

          let obj = {
            id: this.form.id,
            score: this.form.newScore,
            parperId: this.form.parperId,
          }

          setScore(obj).then(response => {
            this.$modal.msgSuccess("修改成功")
            this.open = false
            this.getList()
          })

        }
      })
    },

    // 返回上一页
    goBack() {
      this.$router.go(-1)
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除考试批次编号为"' + ids + '"的数据项？').then(function () {
        return delBitch(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('exa/bitch/export', {
        ...this.queryParams
      }, `bitch_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
